﻿using Red.Controls.ViewModels;
using Red.EngineData.View;
using Red.Toolkit.PhysicalDecorationEditor.ViewModels;
using System;
using System.Windows;
using System.Windows.Controls;

namespace Red.Toolkit.PhysicalDecorationEditor.Menus
{
    /// <summary>
    /// Interaction logic for StructureView.xaml
    /// </summary>
    public partial class StructureView : UserControl
    {
        private StructureViewItemRoot _physicalTreeRoot;

        public StructureView()
        {
            InitializeComponent();
        }


        public void Initialize( DataView rootView )
        {
            RequestReload( rootView );
        }

        private void Add_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            StructureViewPhysicsActorListItem node = ( (FrameworkElement)e.OriginalSource ).DataContext as StructureViewPhysicsActorListItem;

            if ( node != null )
            {
                int tmpCounter = node.Children.Count;

                node.Execute( "insertAt", tmpCounter, "actor_" + tmpCounter );
                node.FetchAndRebuild();
            }
        }

        private void Remove_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            TreeItemEngine node = ( (FrameworkElement)e.OriginalSource ).DataContext as TreeItemEngine;

            if ( node != null )
            {
                StructureViewPhysicsActorListItem parent = node.Parent as StructureViewPhysicsActorListItem;

                if ( parent != null )
                {
                    int childIndex = -1;
                    if ( parent != null && Int32.TryParse( node.View.InteropName.Substring( 1, node.View.InteropName.Length - 2 ), out childIndex ) )
                    {
                        parent.Execute( "removeAt", childIndex );
                        parent.FetchAndRebuild();
                    }
                }
            }
        }

        public int GetSelectedActorIndex()
        {
            TreeItemEngine element = _structureTree.SelectedItem as TreeItemEngine;

            int childIndex = -1;
            if ( element is StructureViewPhysicsActorItem )
            {
                Int32.TryParse( element.View.InteropName.Substring( 1, element.View.InteropName.Length - 2 ), out childIndex );
            }
            return childIndex;
        }

        private void StructureTree_SelectedItemChanged( object sender, RoutedPropertyChangedEventArgs<object> e )
        {
            if ( e.NewValue == null )
            {
                _structureStacker.Visibility = Visibility.Hidden;
                _structureDataCategory.Visibility = Visibility.Hidden;
                return;
            }
            TreeItemEngine element = e.NewValue as TreeItemEngine;
            if ( !element.View.IsCompound || element.View.IsArray )
            {
                _structureDataCategory.Visibility = Visibility.Hidden;
            }

            if ( element.AttachedView != null )
            {
                InteropDataItem.CreateRoot( element.AttachedView, item =>
                {
                    if ( item.IsValid )
                    {
                        _structureDataCategory.DataContext = item;
                        _structureStacker.Visibility = Visibility.Visible;
                        _structureDataCategory.Visibility = Visibility.Visible;
                    }
                    else
                    {
                        _structureDataCategory.Visibility = Visibility.Hidden;
                    }
                } );
            }
            else
            {
                _structureDataCategory.Visibility = Visibility.Hidden;
            }
        }

        internal void RequestReload( DataView dataView )
        {
            _physicalTreeRoot = new StructureViewItemRoot( dataView );
            _structureTree.ItemsSource = _physicalTreeRoot.Children;
        }

        internal void RequestRootSync()
        {
            _physicalTreeRoot.FetchAndRebuild();
        }

        private void AddCollider_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            StructureViewPhysicsColliderListItem node = ((FrameworkElement)e.OriginalSource).DataContext as StructureViewPhysicsColliderListItem;

            if (node != null)
            {
                int tmpCounter = node.Children.Count;

                // Will be redone with actions
                //node.View.ExecuteAction("insertAt", tmpCounter, "shape_" + tmpCounter);
                node.FetchAndRebuild();
            }
        }

        private void RemoveCollider_Executed( object sender, System.Windows.Input.ExecutedRoutedEventArgs e )
        {
            TreeItemEngine node = ((FrameworkElement)e.OriginalSource).DataContext as TreeItemEngine;

            if (node != null)
            {
                StructureViewPhysicsColliderListItem parent = node.Parent as StructureViewPhysicsColliderListItem;

                if (parent != null)
                {
                    int childIndex = -1;
                    if (parent != null && Int32.TryParse(node.View.InteropName.Substring(1, node.View.InteropName.Length - 2), out childIndex))
                    {
                        // Will be redone with actions

                     //   parent.View.ExecuteAction("removeAt", childIndex);
                        parent.FetchAndRebuild();
                    }
                }
            }
        }
    }
}
